import time
import logging

import requests

from conf import config
from helper.meituan.sign_helper import SignHelper
from helper.meituan.request_constant import RequestConstant

logger = logging.getLogger("wechat.helper.meituan")


class GetRiderLocation(object):
    """
    获取骑手当前位置类
    """
    def __init__(self, delivery_id, mt_peisong_id):
        self.delivery_id = int(delivery_id)
        self.mt_peisong_id = mt_peisong_id
        self.appkey = config.MEITUAN_APP_KEY
        self.secret = config.MEITUAN_APP_SECRET

    def get_rider_location(self):
        secret = self.secret
        data = self.rider_location_request_data()
        sign = SignHelper.get_sign(data, secret)
        logger.info(f"sign={sign}")
        data["sign"] = sign
        url = RequestConstant.GET_RIDER_LOCATION
        try:
            response = requests.post(url, data=data, timeout=30)
            response = response.json()
            logger.info(f"response={response}")
            return response
        except Exception as e:
            logger.error(e)
            return False

    def rider_location_request_data(self):
        data = dict(
            timestamp=int(time.time()),
            version="1.0",
            appkey=self.appkey,
            delivery_id=self.delivery_id,
            mt_peisong_id=self.mt_peisong_id,
        )
        return data


if __name__ == '__main__':
    delivery_id = "15014215770078694"
    mt_peisong_id = "1558601279095002469"
    o = GetRiderLocation(delivery_id, mt_peisong_id)
    result = o.get_rider_location()
    print(f"result={result}")